using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Threading;


namespace Risiko.Logic
{

  public class RandomAIPlayer : PlayerBehavior
  {
    public RandomAIPlayer(Game game)
      : base(game)
    {
      this.EnabledChanged += new EventHandler<EventArgs>(RandomAIPlayer_EnabledChanged);
    }

    int num_Attacks = 0;
    void RandomAIPlayer_EnabledChanged(object sender, EventArgs e)
    {
      if (Enabled)
      {
        hasNextMove = Logic.HasNextMove.Thinking;
        hasNextAttack = Logic.HasNextMove.Thinking;
        hasStrategicMove = Logic.HasNextMove.No;
        hasConquerMove = Logic.HasNextMove.No;

        num_Attacks = 1;
        PlaceReinforcements();
        gameState.playerStates[(int)selfID].CurrCardMove = base.ComputeCardCombinations();
        if (gameState.playerStates[(int)selfID].CurrCardMove.isValid)
        {
          hasCardMove = HasNextMove.Yes;
        }
        StartNextAttackAsync();
        //StrategicMove();
      }
    }

    private void StrategicMove()
    {
      //StrategicMoveArmies.from = 
      //StrategicMoveArmies.to = 
      //StrategicMoveArmies.numArmies = 
    }

    private void PlaceReinforcements()
    {
      hasNextMove = Logic.HasNextMove.Thinking;

      gameState.playerStates[(int)selfID].CurrArmiesPlacements.Clear();           

      gameState.board.IterTerritories(t =>
        {
          if (t.owner == selfID)
          {
            if (gameState.playerStates[(int)selfID].reinforcementsNum > 0)
            {
              var placement = new ArmiesPlacement() { territory = t, numArmies = 1 };
              gameState.playerStates[(int)selfID].CurrArmiesPlacements.Add(placement);
              gameState.playerStates[(int)selfID].reinforcementsNum--;
            }
          }
        });

      hasNextMove = Logic.HasNextMove.Yes;
    }


    private void StartNextAttackAsync()
    {
      if (num_Attacks != 1)
      {
        hasNextAttack = Logic.HasNextMove.No;
        return;
      }

      hasNextAttack = Logic.HasNextMove.Thinking;

      List<State.Territory> territories = new List<State.Territory>();
      gameState.board.IterTerritories(t =>
        {
          if (t.owner == selfID)
            territories.Add(t);
        });

      new Thread(() =>
      {
        Thread.Sleep(1000);
        for (int i = 0; i < territories.Count; i++)
        {
          var t = territories[i];

          if (t.armies >= 2)
            for (int j = 0; j < t.neighbors.Length; j++)
            {
              int ArmiesAttack = 1;
              if (t.armies == 3) ArmiesAttack = 2;
              if (t.armies > 3) ArmiesAttack = 3;

              var n = t.neighbors[j];
              var t1 = gameState.board.GetTerritory(n);
              if (t1.owner != selfID)
              {
                nextAttack = new Attack()
                {
                  from = new State.TerritoryReference() { continentID = t.continentID, territoryID = t.territoryID },
                  to = new State.TerritoryReference() { continentID = t1.continentID, territoryID = t1.territoryID },
                  numArmies = ArmiesAttack
                };

                hasNextAttack = Logic.HasNextMove.Yes;
                return;
              }
            }
          Thread.Sleep(10);
        }

        hasNextAttack = Logic.HasNextMove.No;
        return;
      }).Start();
    }

    public override void Initialize()
    {
      base.Initialize();
    }

    public override void Update(GameTime gameTime)
    {
      base.Update(gameTime);
    }

    public Attack nextAttack;
    //public override Attack CurrAttack
    //{
    //  get { return gameState.playerStates[(int)selfID].CurrAttack; }
    //}

    public override void OnCurrentAttackWon()
    {
      hasNextAttack = HasNextMove.Thinking;
      num_Attacks = 1;
      StartNextAttackAsync();
      base.OnCurrentAttackWon();
    }

    public override void OnCurrentAttackLost()
    {
      hasNextAttack = HasNextMove.No;
      num_Attacks++;
      //StartNextAttackAsync();
    }
  }
}
